package cn.raven.pet.controller;

import cn.raven.basic.annotation.PreAuthorize;
import cn.raven.org.domain.Department;
import cn.raven.pet.service.IPetTypeService;
import cn.raven.pet.domain.PetType;
import cn.raven.pet.query.PetTypeQuery;
import cn.raven.basic.utils.AjaxResult;
import cn.raven.basic.utils.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/petType")
public class PetTypeController {
    @Autowired
    public IPetTypeService petTypeService;


    /**
     * 保存和修改公用的
     * @param petType  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    @PreAuthorize(name = "保存petType",sn = "petType:save")
    public AjaxResult save(@RequestBody PetType petType){
        try {
            if( petType.getId()!=null)
                petTypeService.update(petType);
            else
                petTypeService.add(petType);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存对象失败！"+e.getMessage());
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    @PreAuthorize(name = "删除petType",sn = "petType:delete")
    public AjaxResult del(@PathVariable("id") Long id){
        try {
            petTypeService.del(id);
            return AjaxResult.me();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除对象失败！"+e.getMessage());
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    @PreAuthorize(name = "id查询petType",sn = "petType:id")
    public PetType findById(@PathVariable("id")Long id)
    {
        return petTypeService.findOne(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<PetType> findAll(){

        return petTypeService.findAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    @PreAuthorize(name = "petType列表",sn = "petType:list")
    public PageList<PetType> queryData(@RequestBody PetTypeQuery query)
    {
        return petTypeService.queryData(query);
    }

    /**
    * @Title: tree
    * @Description: 宠物类型树
    * @Author: king
    * @Version: 1.0
    * @Date:  2022/7/16 12:46
    * @Parameters: []
    * @Return java.util.List<cn.raven.pet.domain.PetType>
      */
    @GetMapping("/tree")
    @PreAuthorize(name = "上级部门树",sn = "petType:tree")
    public List<PetType> tree(){
        return petTypeService.tree();
    }

    /**
    * @Title: batchDel
    * @Description: 批量删除
    * @Author: king
    * @Version: 1.0
    * @Date:  2022/7/16 22:58
    * @Parameters: [ids]
    * @Return cn.raven.basic.utils.AjaxResult
      */
    @PatchMapping("/batchDel")
    @PreAuthorize(name = "批量删除宠物类型",sn = "petType:batchDel")
    public AjaxResult batchDel(@RequestBody Long[] ids){
        try {
            petTypeService.batchDel(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("系统繁忙,请骚后重试").setSuccess(false);
        }
    }
}
